<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <h1>Picking Objects</h1>
    <h2>Double-click objects to pick them</h2>
    <p>In this example, we highlight and view-fit each object we double-click.</p>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

import {overlappingPick} from "../libs/overlappingPick.js";
import {math, Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

const canvas = window.document.getElementById("myCanvas");

const viewer = new Viewer({ canvasElement: canvas });

const camera = viewer.camera;
viewer.camera.eye = [-3.93, 2.85, 27.01];
viewer.camera.look = [4.40, 3.72, 8.89];
viewer.camera.up = [-0.01, 0.99, 0.039];

new XKTLoaderPlugin(viewer).load({
    id: "myModel",
    src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
    edges: true
});

const addStillPressListener = function(element, onPress) {
    const pressPrecision = 3;
    let downClientCoords = null;

    element.addEventListener("mousedown", function(event) {
        if (event.which === 1) {
            downClientCoords = math.vec2([ event.clientX, event.clientY ]);
        }
    });

    element.addEventListener("mousemove", function(event) {
        if (downClientCoords && (math.distVec2([ event.clientX, event.clientY ], downClientCoords) > pressPrecision)) {
            downClientCoords = null;
        }
    });

    element.addEventListener("mouseup", function(event) {
        if (downClientCoords && (math.distVec2([ event.clientX, event.clientY ], downClientCoords) > pressPrecision)) {
            downClientCoords = null;
        }

        if ((event.which === 1) && downClientCoords) {
            onPress(event);
        }
    });
};

viewer.cameraControl.doublePickFlyTo = false;
addStillPressListener(
    canvas,
    (function() {
        let highlightedEntity = null;

        return function(event) {
            const origin = math.vec3();
            const direction = math.vec3();
            math.canvasPosToWorldRay(canvas, camera.viewMatrix, camera.projMatrix, camera.projection, [ event.clientX, event.clientY ], origin, direction);

            const pickResult = overlappingPick(viewer.scene, { origin: origin, direction: direction }, { wrapAround: true });

            if (highlightedEntity) {
                highlightedEntity.highlighted = false;
            }

            highlightedEntity = pickResult && pickResult.entity;
            console.log(highlightedEntity && highlightedEntity.id);

            if (highlightedEntity) {
                highlightedEntity.highlighted = true;
            }
        };
    })());

</script>
</html>
